/*
 * Copyright (C), 2002-2016, 上海亦寻信息技术有限公司
 * FileName: RequestUtil.java
 * Author:   linyao
 * Date:     2016-10-17 上午11:20:55
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.common.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.emm.yixun.common.enums.ResponseEnums;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author linyao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class RequestUtil {
	private static Logger logger = LoggerFactory.getLogger(RequestUtil.class);
	private static long startTime = 0L;
	private static long endTime = 0L;

	/**
	 * 接口请求 功能描述: <br>
	 * 〈功能详细描述〉
	 * 
	 * @param url
	 * @param params
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@SuppressWarnings("deprecation")
	public static ResponseBody buildRequest(String params, String url,String clientKey,String seq,String digest) throws  Exception {
		ResponseBody responseBody = new ResponseBody();
		PutMethod method = new PutMethod(url);
		HttpClient httpClient = new HttpClient();
		int statusCode = 0;
		logger.info("请求路径：{}", url);
		method.addRequestHeader("Content-Type", "application/json");
		method.addRequestHeader("clientKey",clientKey);
		method.addRequestHeader("seq",seq);
		method.addRequestHeader("digest",digest);
		method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		method.setRequestBody(params);
		startTime = System.currentTimeMillis();
		statusCode=httpClient.executeMethod(method);
		endTime = System.currentTimeMillis();

		logger.info("statusCode:" + statusCode);
		logger.info("调用API 花费时间(单位：毫秒)：" + (endTime - startTime));
		if(statusCode==200){
			responseBody.setCode(ResponseEnums.RESPONSEENUMS_000000.getCode());
			responseBody.setDesc("成功");
		}else{
			String body = method.getResponseBodyAsString();
			responseBody = JSON.parseObject(body, ResponseBody.class);
		}
		return responseBody;
	}

	public static void main(String[] args) {
	}

}
